﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.BLL;
using BotonJXC.Entity;
using NHibernate;
using BotonJXC.DAO;

namespace BotonJXC.Forms
{
    public partial class LoginForm : BaseForm
    {
        AccountBLL _accountBll = new AccountBLL();
        RoleDataAuthBLL _roleDataAuthBll = new RoleDataAuthBLL();
        WarehouseBLL _warehouseBll = new WarehouseBLL();
        /// <summary>
        /// 获取当前登录用户
        /// </summary>
        public static Account Account { get; private set; }
        public static int UserId
        {
            get { return LoginForm.Account.UserId; }
        }
        /// <summary>
        /// 获取当前登录用户的工作组
        /// </summary>
        public static Role Role { get; private set; }
        /// <summary>
        /// 获取当前登录用户的所属门店
        /// </summary>
        public static Store Store { get; private set; }
        public static int StoreId
        {
            get { return LoginForm.Store.StoreId; }
        }
        /// <summary>
        /// 获取当前登录用户的所属部门
        /// </summary>
        public static Department Department { get; private set; }
        public static int DepId
        {
            get { return LoginForm.Department.DepId; }
        }
        public LoginForm()
        {
            InitializeComponent();
            klInfo.ForeColor = Color.Red;
            kcbUsername.SelectedIndex = 0;
        }

        private void kBtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool CheckLogin()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                Account user = _accountBll.CheckLogin(kcbUsername.Text, ktbPassword.Text);
                if (user != null)
                {
                    LoginForm.Account = user;
                    LoginForm.Role = user.Role;
                    LoginForm.Department = user.Department;
                    LoginForm.Store = user.Department.Store;
                    SysContext.CurrentUserDataAuth = _roleDataAuthBll.FindByRoleId(LoginForm.Role.RoleId);
                    SysContext.CurrentUserWarehouses = _warehouseBll.FindAll();
                    MainFrame mf = (MainFrame)this.MdiParent;
                    mf.SetCurrentUserInfo(user);
                    return true;
                }
                else
                {
                    klInfo.Visible = true;
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            return false;
        }

        private void kBtnOK_Click(object sender, EventArgs e)
        {
            if (CheckLogin())
            {
                MainFrame mf = (MainFrame)this.MdiParent;
                mf.LoadMainMenu();
                mf.LoadLeftMenus();
                this.Close();
            }
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            //ProductForm pf = new ProductForm();
            //pf.DoubleListRowAction = ActionTypes.DoubleToReturn;
            //pf.ProductSelected += new Forms.Events.ProductSelectedEventHandler(pf_ProductSelected);
            //pf.Show();
            TestBinarySearch();
        }

        void pf_ProductSelected(object sender, Events.ProductSelectedEventArgs e)
        {
            MessageBox.Show(e.SelectedProduct.ProductName);
        }
        public int binarySearch(int[] srcArray, int des)
        {
            int low = 0;
            int high = srcArray.Length - 1;
            while (low <= high)
            {
                int middle = (low + high) / 2;
                if (des == srcArray[middle])
                {
                    return middle;
                }
                else if (des < srcArray[middle])
                {
                    high = middle - 1;
                }
                else
                {
                    low = middle + 1;
                }
            }
            return -1;
        }
        public int findInsertPos(int[] srcArray, int des)
        {
            int low = 0;
            int high = srcArray.Length - 1;
            while (low <= high)
            {
                int middle = (low + high) / 2;
                if (des == srcArray[middle])
                {
                    return middle + 1;
                }
                else if (des < srcArray[middle])
                {
                    high = middle - 1;
                }
                else
                {
                    low = middle + 1;
                }
            }
            Console.WriteLine("{0},{1}", low, high);
            return low;
        }
        public void TestBinarySearch()
        {
            int[] src = new int[] { 1, 3, 5, 5, 7, 8, 8, 9, 9, 10 };
            kryptonLabel3.Text = "index: " + findInsertPos(src, Util.Utils.ToInt(kcbUsername.Text, 0));
        }
    }
}
